package org.dakuo.renrenTool;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

public class RenrenNewStatus {

	static String statusHostUrl = "http://status.renren.com/doing/update.do";

	private static boolean postNewStatus(List<Cookie> cookies) {

		try {
			DefaultHttpClient httpClient = new DefaultHttpClient();
			Iterator<Cookie> ci = cookies.iterator();
			while (ci.hasNext())
				httpClient.getCookieStore().addCookie(ci.next());

			HttpPost httpPost = new HttpPost(statusHostUrl);

			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			nvps.add(new BasicNameValuePair("c", "测试"));
			nvps.add(new BasicNameValuePair("raw", "测试"));
			nvps.add(new BasicNameValuePair("isAtHome", "1"));
			httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

			httpPost
					.setHeader(
							"User-Agent",
							"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)");
			httpPost
					.addHeader("Accept",
							"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
			httpPost.addHeader("Accept-Language", "en-us,en;q=0.5");
			httpPost.addHeader("Accept-Encoding", "gzip,deflate");
			httpPost.addHeader("Accept-Charset",
					"ISO-8859-1,utf-8;q=0.7,*;q=0.7");
			httpPost.addHeader("Content-Type",
					"application/x-www-form-urlencoded");
			httpPost.addHeader("Referer",
					"http://status.renren.com/ajaxproxy.htm");

			HttpResponse response = httpClient.execute(httpPost);
			HttpEntity entity = response.getEntity();

			System.out.println("New Status Post: " + response.getStatusLine());
			if (entity != null) {
				entity.consumeContent();
			}
			httpClient.getConnectionManager().shutdown();
			return true;

		} catch (Exception e) {

			System.out.println(e.getStackTrace());
			return false;
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String email = "xymlem@hotmail.com";
		String password = "lemxym";
		List<Cookie> cookieList = RenrenLogin.login(email,password);
		if (cookieList != null) {
			if (postNewStatus(cookieList))
				System.out.print("Well post");
			else
				System.out.print("Fail post");
		}
	}

}
